//
//  ShopHeaderView.m
//  YIChuSeller
//
//  Created by xsj on 2018/11/22.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXShopHeaderView.h"
#import "GXHomeChangeNameController.h"
#import "UIButton+IconPosition.h"
#import <AVFoundation/AVCaptureDevice.h>
#import <Photos/PHPhotoLibrary.h>
#import "ImagePickerTool.h"
#import "OrderAndUploadPhotoMananger.h"
#import "GXOrderPopController.h"
#import "NewsAndAdviceManager.h"
@interface GXShopHeaderView ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate>
@property (weak, nonatomic) IBOutlet UIView *selectView;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *headBGView;
@property (nonatomic,strong) UIButton * selectBtn;
@property (weak, nonatomic) IBOutlet UIImageView *userBGView;
@property (nonatomic,strong) NSData *imageData;
@property(nonatomic,strong) GXOrderPopController *oderPopView;
@end
@implementation GXShopHeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
+(instancetype)headView
{
    return [[NSBundle mainBundle]loadNibNamed:@"GXShopHeaderView" owner:nil options:nil].lastObject;
}
-(void)awakeFromNib
{
    [super awakeFromNib];
    [self creatSelectBtn];
}
#pragma 创建选择状态的按钮
- (void)creatSelectBtn
{
    UIButton *selectbutton = [UIButton buttonWithType:UIButtonTypeCustom];
    [selectbutton setImage:[UIImage imageNamed:@"home_header_buttonup"] forState:UIControlStateNormal];
    [selectbutton setImage:[UIImage imageNamed:@"home_header_buttondown"] forState:UIControlStateSelected];
    selectbutton.titleLabel.font = [UIFont systemFontOfSize:13];
    selectbutton.backgroundColor = [UIColor redColor];
    [selectbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [selectbutton setBackgroundColor:[UIColor clearColor]];
    [selectbutton setTitle:@"不可预约" forState:UIControlStateNormal];
    selectbutton.frame = CGRectMake(kUIScreenWidth-110, Height_StatusBar, 95, 40);
    selectbutton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    self.selectBtn = selectbutton;
    [selectbutton setIconInRightWithSpacing:3];
    [self addSubview:selectbutton];
    [selectbutton addTarget:self  action:@selector(pressSelectBtn:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma 选择状态按钮的点击事件
- (void)pressSelectBtn:(UIButton *)button {
    NSString * shopAuthState = _accountModel.users[@"auditState"];
    if (![shopAuthState isEqualToString:@"1"]) {
        [SVProgressHUD showErrorWithStatus:@"请先进行店铺认证"];
        return;
    }
    [self orderAction];
}
- (void)setAccountModel:(AccountModel *)accountModel
{
    _accountModel = accountModel;
    NSDictionary * shopDict = [NSDictionary dictionaryWithDictionary:accountModel.sotres];
    NSDictionary * userDict = [NSDictionary dictionaryWithDictionary:accountModel.users];
    _accountModel = accountModel;
    NSString * headUrlStr = [NSString stringWithFormat:@"%@/%@",kBaseURLIMAGE,shopDict[@"backImage"]];
    NSString * userUrlStr = [NSString stringWithFormat:@"%@/%@",kBaseURLIMAGE,userDict[@"userLogo"]];
    [self.headBGView sd_setImageWithURL:[NSURL URLWithString:headUrlStr] placeholderImage:[UIImage imageNamed:@"placeholder_cover"]];
    [self.userBGView sd_setImageWithURL:[NSURL URLWithString:userUrlStr] placeholderImage:[UIImage imageNamed:@"placeHolder_userLogo"]];
    self.nameLabel.text = userDict[@"name"];
    NSString * shopState = [NSString stringWithFormat:@"%@",shopDict[@"sState"]];
    if ([shopState isEqualToString:@"-1"]) {
        self.selectBtn.enabled = NO;
    }
    [self.selectBtn setTitle:[self selectButtonStateFromStr:shopState] forState:UIControlStateNormal];
    [self.selectBtn setIconInRightWithSpacing:3];
}
#pragma 状态
- (NSString *)selectButtonStateFromStr:(NSString *)str
{
    if ([str isEqualToString:@"0"]) {
        return @"营业中";
    }else if ([str isEqualToString:@"1"])
    {
        return @"休息中";
    }else if ([str isEqualToString:@"2"])
    {
        return @"可预约";
    }else if ([str isEqualToString:@"3"])
    {
        return @"不可预约";
    }else if ([str isEqualToString:@"4"])
    {
        return @"封停状态";
    }else if ([str isEqualToString:@"-1"])
    {
        return @"无店铺";
    }
    return nil;
}

- (IBAction)pressChangeNameBtn:(UIButton *)sender {
    GXHomeChangeNameController * changeVC = [[GXHomeChangeNameController alloc]init];
    [self.getViewController.navigationController pushViewController:changeVC animated:YES];
    
}
- (IBAction)pressSelectPhotoBtn:(UIButton *)sender {
    [[ImagePickerTool sharePicker] showImagePicker:self.getViewController allowEdit:YES complete:^(id param) {
        [OrderAndUploadPhotoMananger uploadPhotoToServerFromImage:param fileName:UserLogo success:^(id responseObject) {
            if (responseObject) {
                self.userBGView.image = param;
            }
        } fail:^(id responseObject) {
            
        }];
    }];
}

-(void)orderAction {
     WeakSelf(weakSelf);
    if (self.oderPopView == NULL) {
        self.oderPopView = [[GXOrderPopController alloc] init];
        self.oderPopView.popButtonBlock = ^(UIButton * popButton) {
            NSString * stateStr = nil;
            if ([popButton.titleLabel.text isEqualToString:@"营业中"])
            {
                stateStr = @"0";
            }else if ([popButton.titleLabel.text isEqualToString:@"休息中"])
            {
                stateStr = @"1";
            }
            [NewsAndAdviceManager changeShopOrderStateWithStoreID:StoreId state:stateStr success:^(id responseObject) {
                if (responseObject) {
                    [weakSelf.selectBtn setTitle:popButton.titleLabel.text forState:UIControlStateNormal];
                    weakSelf.selectBtn.selected = !weakSelf.selectBtn.selected;
                    
                }
            } fail:^(id responseObject) {

            }];
            
        };
        
    };
    
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self.oderPopView.view];
}


@end
